import static org.junit.Assert.*;

import java.util.HashMap;
import java.util.Map;

import org.junit.Test;

/*
 * @author Maura Gerke
 * @version 11.30.13
 */
public class HuffmanTests {

	@Test
	public void testFreqTable1() {
		String str = "happythanksgiving";
		Map<Character, Integer> testMap = new HashMap<Character,Integer>();
		testMap.put('h', 2);
		testMap.put('a', 2);
		testMap.put('p', 2);
		testMap.put('y', 1);
		testMap.put('t', 1);
		testMap.put('n', 2);
		testMap.put('k', 1);
		testMap.put('s', 1);
		testMap.put('g', 2);
		testMap.put('i', 2);
		testMap.put('v', 1);
		Map<Character, Integer> codeMap = Huffman.buildFrequencyTable(str);
		assertTrue(codeMap.equals(testMap));
	}
	
	@Test
	public void testFreqTable2() {
		String str = "abbcccdddd";
		Map<Character, Integer> testMap = new HashMap<Character,Integer>();
		testMap.put('a', 1);
		testMap.put('b', 2);
		testMap.put('c', 3);
		testMap.put('d', 4);
		Map<Character, Integer> codeMap = Huffman.buildFrequencyTable(str);
		assertTrue(codeMap.equals(testMap));
	}
	
	@Test
	public void test1() {
		String str = "a";
		Map<Character, Integer> testMap = new HashMap<Character,Integer>();
		testMap.put('a', 1);
		Map<Character, Integer> codeMap = Huffman.buildFrequencyTable(str);
		assertTrue(codeMap.equals(testMap));
		
		Map<Character, String> codeTable = Huffman.buildEncoder(codeMap);
		HuffmanTree tree = Huffman.buildHuffmanTree(codeMap);
		String code = Huffman.encodeString(str, codeTable);
		String decode = Huffman.decodeString(code, tree);
		assertTrue(str.equals(decode));
	}
	
	@Test
	public void test2() {
		String str = "qqqqqqqqqqqqqqqqqq";
		Map<Character,Integer> testMap = new HashMap<Character,Integer>();
		Map<Character,Integer> testMap2 = new HashMap<Character,Integer>();
		testMap.put('q', 18);
		testMap2.put('q', 1);
		Map<Character,Integer> codeMap = Huffman.buildFrequencyTable(str);
		assertTrue(codeMap.equals(testMap));
		assertFalse(codeMap.equals(testMap2));
		
		Map<Character, String> codeTable = Huffman.buildEncoder(codeMap);
		HuffmanTree tree = Huffman.buildHuffmanTree(codeMap);
		String code = Huffman.encodeString(str, codeTable);
		String decode = Huffman.decodeString(code, tree);
		System.out.println("code: " + code + " decode: " + decode);
		assertTrue(str.equals(decode));
	}
	
	@Test
	public void test3() {
		String str = "does it work, with spaces & punctuation!?";
		Map<Character,Integer> codeMap = Huffman.buildFrequencyTable(str);
		Map<Character, String> codeTable = Huffman.buildEncoder(codeMap);
		HuffmanTree tree = Huffman.buildHuffmanTree(codeMap);
		String code = Huffman.encodeString(str, codeTable);
		String decode = Huffman.decodeString(code, tree);
		assertTrue(str.equals(decode));
	}
	
	@Test
	public void test4() {
		String str = "";
		Map<Character,Integer> codeMap = Huffman.buildFrequencyTable(str);
		Map<Character, String> codeTable = Huffman.buildEncoder(codeMap);
		HuffmanTree tree = Huffman.buildHuffmanTree(codeMap);
		String code = Huffman.encodeString(str, codeTable);
		String decode = Huffman.decodeString(code, tree);
		assertTrue(str.equals(decode));
	}
}
